/* 
  // 数据库操作
*/

// 1. 连接数据库
const connect = require('./connect')

connect.run()

// 2. 创建骨架

const { shopSchema, registerSchema } = require('./schema/index.js')

// 3. 创建模型

const { shopModel, registerModel } = require('./model/index.js')


const shop_model = shopModel({
  collectionName: 'shops',
  schemaName: shopSchema
})

const register_model = registerModel({
  collectionName: 'registerusers',
  schemaName: registerSchema
})



const db = {
  user: {
    add() {

    },
    del() {

    },
    update() {

    },
    query() {

    }
  },
  register: {
    post(data) {
      return new Promise((resolve, reject) => {
        const register = new register_model(data)
        register_model.find({ 'username': data.username }, (error, docs) => {
          if (error) {
            resolve({
              status: 0,
              errorInfo: error
            })
          } else {
            if (docs.length === 0) {
              register.save(err => {
                if (err) {
                  resolve({
                    status: 1,
                    info: '存储失败'
                  })
                } else {
                  resolve({
                    status: 2,
                    info: '存储成功'
                  })
                }
              })
            } else {
              resolve({
                status: 3,
                info: '此用户已存在'
              })
            }
          }
        })
      })
    },
    get(data) {
      return new Promise((resolve, reject) => {
        register_model.find({ 'username': data.username }, (error, docs) => {
          if (error) {
            resolve({
              status: 0,
              errorInfo: error
            })
          } else {
            if (docs.length === 0) {
              resolve({
                status: 2,
                info: '用户名不存在'
              })
            } else {
              resolve({
                status: 1,
                info: '用户名已存在'
              })
            }
          }
        })
      })
    }
  },
  login: {
    post(data) {
      return new Promise((resolve, reject) => {
        register_model.find({ 'username': data.username }, (err, docs) => {
          if (err) {
            resolve({
              status: 0,
              errInfo: err
            })
          } else {
            if (docs.length === 0) {
              resolve({
                status: 1,
                info: '用户名不存在'
              })
            } else {
              register_model.find({ 'username': data.username, 'password': data.password }, (err, docs) => {
                if (docs.length === 0) {
                  resolve({
                    status: 2,
                    info: '密码对不上'
                  })
                } else {
                  resolve({
                    status: 3,
                    info: '成功找到'
                  })
                }
              })
            }
          }
        })
      })

    }
  },
  shop: {
    add(data) { //增
      data.shop_feture = {
        shop_fetures_flag_index_1: data.shop_fetures_flag_index_1,
        shop_fetures_flag_index_2: data.shop_fetures_flag_index_2,
        shop_fetures_flag_index_3: data.shop_fetures_flag_index_3,
        shop_fetures_flag_index_4: data.shop_fetures_flag_index_4,
        shop_fetures_flag_index_5: data.shop_fetures_flag_index_5,
        shop_fetures_flag_index_6: data.shop_fetures_flag_index_6
      }

      delete data.shop_fetures_flag_index_1
      delete data.shop_fetures_flag_index_2
      delete data.shop_fetures_flag_index_3
      delete data.shop_fetures_flag_index_4
      delete data.shop_fetures_flag_index_5
      delete data.shop_fetures_flag_index_6

      console.log(data)

      return new Promise((resolve, reject) => {
        const shop = new shop_model(data)
        shop_model.find({}, (error, docs) => {
          let flag = true
          docs.forEach(item => {
            if ((item.shop_name === data.shop_name) && (item.shop_address === data.shop_address) && (item.shop_tel == data.shop_tel)) {
              flag = false
              return
            }
          })

          if (flag) {

            //条件成立 ，没有找到数据，可以存储

            // 将数据保存在了数据库
            shop.save(error => {
              if (error) {
                // 存储失败
                resolve(0)
              } else {
                // 表示存储成功
                resolve(1)
              }
            })

          } else {
            //如果这个不成立，找到了数据，不可以存储
            resolve(2)
          }
        })

      })
    },
    delete(data) { // 删
      new Promise((resolve, reject) => {

        shop_model.findById(data.id, (error, docs) => {
          docs.remove((error) => {
            if (error) {
              //删除失败 
              resolve({
                status: 0
              })
            } else {
              // 删除成功
              shop_model.find({}, (error, data) => {
                resolve({
                  status: 1,
                  data
                })
              })
            }
          })
        })

      })
    },
    modify(data) { // 改
      return new Promise((resolve, reject) => {
        shop_model.findById(data._id, (error, docs) => {
          docs.shop_name = data.shop_name
          docs.shop_tel = data.shop_tel
          docs.save((error) => {
            if (error) {
              //修改失败了
              resolve(0)
            } else {
              resolve(1)
            }
          })
        })
      })
    },
    query() { // 查
      return new Promise((resolve, reject) => {

        shop_model.find({}, (error, docs) => {
          if (error) {
            // 查询出了问题 
            resolve({
              status: 0,
              errorInfo: error
            })

          } else {
            //查询没问题，没问题就返回结果
            resolve({
              status: 1,
              data: docs
            })
          }
        })

      })
    }
  }

}

module.exports = db